package com.baidu.eureka.config;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;

/**
 * @author caixiaofeng
 * @version 1.0
 * @Description: <<Swagger接口文档配置类>>
 * @company 浙江优创信息技术有限公司
 * @create 2020-07-18 19:01
 */
@Configuration
@EnableSwagger2
@EnableKnife4j
@Import(BeanValidatorPluginsConfiguration.class)
public class SwaggerConfiguration {
    @Value("${spring.profiles.active}")
    private  String active;

    //====================================================>>用户分组-负责人：蔡晓峰
    /**
     * 用户分组
     *
     * @return
     */
    @Bean(value = "userGroup")
    public Docket userGroup() {
        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo1())
                //是否开启api文档
                .enable("test".equals(active) || "dev".equals(active))
                //分组名称
                .groupName("用户操作")
                .select()
                //这里指定Controller扫描包路径
                .apis(RequestHandlerSelectors.basePackage("com.baidu.eureka.controller.user"))
                .paths(PathSelectors.any())
                .build();
        return docket;
    }

    /**
     * 蔡晓峰个人信息
     *
     * @return
     */
    private ApiInfo apiInfo1() {
        //联系人
        Contact contact = new Contact(
                //联系人
                "蔡晓锋",
                //网址
                "http://localhost:8081/swagger-ui.html#/",
                //邮箱
                "1941338475@qq.com");
        //描述消息
        return new ApiInfo(
                "xxx项目api文档",//标题
                "xxx描述消息",//描述消息
                "1.0",//版本号
                "https://www.baidu.com/",//服务条款网址
                contact,//联系人消息
                "Apache 2.0",//许可证
                "https://www.apache.org/licenses/LICENSE-2.0",//许可证网址
                new ArrayList<>());//供应商扩展
    }
    //====================================================>>角色分组-负责人：谢霆锋

    /**
     * 角色分组
     *
     * @return
     */
    @Bean(value = "roleGroup")
    public Docket roleGroup() {
        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo2())
                //是否开启api文档
                .enable("test".equals(active) || "dev".equals(active))
                //分组名称
                .groupName("角色操作")
                .select()
                //这里指定Controller扫描包路径
                .apis(RequestHandlerSelectors.basePackage("com.baidu.eureka.controller.role"))
                .paths(PathSelectors.any())
                .build();
        return docket;
    }

    /**
     * 谢霆锋个人信息
     *
     * @return
     */
    private ApiInfo apiInfo2() {
        //联系人
        Contact contact = new Contact(
                //联系人
                "谢霆锋",
                //网址
                "http://localhost:8081/swagger-ui.html#/",
                //邮箱
                "1941338475@qq.com");
        //描述消息
        return new ApiInfo(
                "xxx项目api文档",//标题
                "xxx描述消息",//描述消息
                "1.0",//版本号
                "https://www.baidu.com/",//服务条款网址
                contact,//联系人消息
                "Apache 2.0",//许可证
                "https://www.apache.org/licenses/LICENSE-2.0",//许可证网址
                new ArrayList<>());//供应商扩展
    }
    //====================================================>>资源权限分组-负责人：刘德华

    /**
     * 资源权限
     *
     * @return
     */
    @Bean(value = "permissionGroup")
    public Docket permissionGroup() {
        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo3())
                //是否开启api文档
                .enable("test".equals(active) || "dev".equals(active))
                //分组名称
                .groupName("资源权限操作")
                .select()
                //这里指定Controller扫描包路径
                .apis(RequestHandlerSelectors.basePackage("com.baidu.eureka.controller.permission"))
                .paths(PathSelectors.any())
                .build();
        return docket;
    }

    /**
     * 刘德华个人信息
     *
     * @return
     */
    private ApiInfo apiInfo3() {
        //联系人
        Contact contact = new Contact(
                //联系人
                "刘德华",
                //网址
                "http://localhost:8081/swagger-ui.html#/",
                //邮箱
                "1941338475@qq.com");
        //描述消息
        return new ApiInfo(
                "xxx项目api文档",//标题
                "xxx描述消息",//描述消息
                "1.0",//版本号
                "https://www.baidu.com/",//服务条款网址
                contact,//联系人消息
                "Apache 2.0",//许可证
                "https://www.apache.org/licenses/LICENSE-2.0",//许可证网址
                new ArrayList<>());//供应商扩展
    }
}
